package dao.server;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class TestScript {
	private int id;
	private int idProject;
	private String name;
	private String time;
	private String content;
	private int code;
	private String checkOut;
	
	public TestScript(){
		name = "";
		time = "";
		checkOut = "";
		content = "";
	}

	public TestScript(String name, String time, String content) {
		this.name = name;
		this.time = time;
		this.checkOut = "";
		this.content = content;
	}
	public String getCheckOut(){
		return checkOut;
	}
	public void setCheckOut(String checkOut){
		this.checkOut = checkOut;
	}
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getIdProject() {
		return idProject;
	}

	public void setIdProject(int idProject) {
		this.idProject = idProject;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}
	public StringWriter objectToXml(DocumentBuilder docBuilder) {
		try {
			// document elements
			Document doc = docBuilder.newDocument();
			Element document = doc.createElement("document");
			doc.appendChild(document);

			// set attribute to document element
			Attr type = doc.createAttribute("type");
			type.setValue("TESTSCRIPT");
			document.setAttributeNode(type);

			Attr action = doc.createAttribute("action");
			action.setValue("");
			document.setAttributeNode(action);

			// head elements
			Element head = doc.createElement("head");
			document.appendChild(head);

			// id elements
			Element id = doc.createElement("id");
			id.appendChild(doc.createTextNode(String.valueOf(this.getId())));
			head.appendChild(id);

			// id_project elements
			Element idProject = doc.createElement("id_project");
			idProject.appendChild(doc.createTextNode(String.valueOf(this
					.getIdProject())));
			head.appendChild(idProject);

			// name elements
			Element name = doc.createElement("name");
			name.appendChild(doc.createTextNode(this.getName()));
			head.appendChild(name);

			// time elements
			Element time = doc.createElement("time");
			time.appendChild(doc.createTextNode(this.getTime()));
			head.appendChild(time);

			// code elements
			Element code = doc.createElement("code");
			code.appendChild(doc.createTextNode(String.valueOf(this.getCode())));
			head.appendChild(code);
			
			// check out elements
			Element checkOut = doc.createElement("checkout");
			checkOut.appendChild(doc.createTextNode(this.getCheckOut()));
			head.appendChild(checkOut);

			// body elements
			Element body = doc.createElement("body");
			document.appendChild(body);
			body.appendChild(doc.createTextNode(this.getContent()));

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			transformer.transform(source, result);
			return sw;
		} catch (Exception e) {
			//e.printStackTrace();
			return null;
		}
	}
	public void xmlToObject(Document doc){
		try {	
			NodeList nodeList = doc.getElementsByTagName("head");
			Element element = (Element)nodeList.item(0);
			try{
				this.setId(Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setId(0);
			}
			try{
				this.setIdProject(Integer.parseInt(element.getElementsByTagName("id_project").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setIdProject(0);
			}
			this.setName(element.getElementsByTagName("name").item(0).getTextContent());
			this.setTime(element.getElementsByTagName("time").item(0).getTextContent());			
			try{
				this.setCode(Integer.parseInt(element.getElementsByTagName("code").item(0).getTextContent()));
			}catch(NumberFormatException nfe){
				this.setCode(0);
			}
			this.setCheckOut(element.getElementsByTagName("checkout").item(0).getTextContent());
			//get content
			NodeList contentList = doc.getElementsByTagName("content");	
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(contentList.item(0));
			StringWriter sw = new StringWriter();
			StreamResult result = new StreamResult(sw);
			transformer.transform(source, result);
			this.setContent(sw.toString());
			this.setContent(content.substring(38));
		} catch (Exception e) {
			//e.printStackTrace();
		}
	}
	public String addTestScript(DAO dao){
		if(dao != null)
			if(dao.addTestScript(this))
				return String.valueOf(id);
			else
				return "Create test script " + this.getName() + " unsuccess, please try again!";
		return "Create test script " + this.getName() + " unsuccess, please try again!";
	}
	public String updateTestScript(DAO dao){
		if(dao != null){
			TestScript ts = dao.getTestScript(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test script";
			if(!this.getCheckOut().equals(ts.getCheckOut()))
				return "You must check out test script " + this.getName() + " before you want to save it";
			if(dao.updateTestScript(this))
				return "Update test script success";						
		}
		return "Update test script unsuccess, please try again!";
	}
	public String renameTestScript(DAO dao){
		if(dao != null){
			TestScript ts = dao.getTestScript(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test script";
			if(dao.renameTestScript(this))
				return "Rename test script success";;
		}
		return "Rename test script unsuccess, please try again!";
	}
	public String deleteTestScript(DAO dao){
		if(dao != null){
			TestScript ts = dao.getTestScript(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test script";
			if(dao.deleteTestScript(ts))
				return "Remove test script " + this.getName() + " success";
		}
		return "Remove test script" + this.getName() + " unsuccess, please try again!";
	}
	public String checkOutTestScript(DAO dao){
		if(dao != null){
			TestScript ts = dao.getTestScript(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test script " + this.getName();
			else
			{
				if(!ts.getCheckOut().equals("") && !ts.getCheckOut().equals(this.getCheckOut()))
					return "This test script is checked out. Please, try again later!";
				if(ts.getCode() != this.getCode())
					return "This test script is old version. Please, refreshing it and try again!";
				if(dao.checkOutTestScript(this))
					return "Check out success";
				else
					return "Check out test script " + this.getName() + " unsuccess, please try again!";
			}
		}
		return "Check out test script " + this.getName() + " unsuccess, please try again!";	
	}
	public String releaseCheckOutTestScript(DAO dao){
		if(dao != null){
			this.setCheckOut("");
			TestScript ts = dao.getTestScript(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test script";
			if(dao.checkOutTestScript(this))
				return "Release check out success";		
		}
		return "Release check out test script " + this.getName() + " unsuccess, please try again!";	
	}
	public String checkInTestScript(DAO dao){
		if(dao != null){
			this.setCheckOut("");
			TestScript ts = dao.getTestScript(this.getId());
			if(ts == null || ts.getId() == 0)
				return "Can't find the test script";
			if(dao.checkOutTestScript(this))
				return "Check in success";			
		}
		return "Check in test script " + this.getName() + " unsuccess, please try again!";	
	}
}
